package com.pro_test.proservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pro_test.commonutils.R;
import com.pro_test.proservice.entity.ProBuild;
import com.pro_test.proservice.entity.vo.BuildQuery;
import com.pro_test.proservice.service.ProBuildService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Captain
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/proservice/pro-build")
@CrossOrigin
public class ProBuildController {
    @Autowired
    private ProBuildService proBuildService;

    //查询楼栋表所有数据
    @ApiOperation(value = "查询所有楼栋")
    @GetMapping("findAll")
    public R findAllBuild()
    {
        List<ProBuild> list = proBuildService.list(null);
        return R.ok().data("items",list);
    }

    //逻辑删除楼栋
    @ApiOperation(value = "逻辑删除楼栋")
    @DeleteMapping("{id}")
    public R removeBuild(@ApiParam(name = "id", value = "楼栋ID", required = true)
                           @PathVariable String id)
    {
        boolean flag = proBuildService.removeById(id);
        if (flag)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    //条件查询分页
    @ApiOperation(value = "条件分页查询")
    @PostMapping("pageBuildCondition/{current}/{limit}")
    public R pageBuildCondition(@PathVariable long current,
                                @PathVariable long limit,
                                @RequestBody(required = false) BuildQuery buildQuery)
    {
        //创建page对象
        Page<ProBuild> pageBuild = new Page<>(current,limit);

        //构建条件
        QueryWrapper<ProBuild> wrapper = new QueryWrapper<>();
        String num = buildQuery.getNum();
        String unit = buildQuery.getUnit();
        String begin = buildQuery.getBegin();
        String end = buildQuery.getEnd();

        if (!StringUtils.isEmpty(num))
        {
            wrapper.like("num",num);
        }
        if (!StringUtils.isEmpty(unit))
        {
            wrapper.eq("unit",unit);
        }
        if (!StringUtils.isEmpty(begin))
        {
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end))
        {
            wrapper.le("gmt_create",end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        proBuildService.page(pageBuild,wrapper);
        long total = pageBuild.getTotal();
        List<ProBuild> records = pageBuild.getRecords();

        return R.ok().data("total",total).data("items",records);
    }

    //添加楼栋
    @ApiOperation(value = "添加楼栋")
    @PostMapping("addBuild")
    public R addBuild(@RequestBody ProBuild proBuild)
    {
        boolean save = proBuildService.save(proBuild);
        if (save)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    //根据楼栋id进行查询
    @ApiOperation(value = "查询楼栋")
    @GetMapping("getBuild/{id}")
    public R getBuild(@PathVariable String id)
    {
        ProBuild proBuild = proBuildService.getById(id);
        return R.ok().data("build",proBuild);
    }

    //修改楼栋
    @ApiOperation(value = "修改楼栋")
    @PostMapping("updateBuild")
    public R updateBuild(@RequestBody ProBuild proBuild)
    {
        boolean flag = proBuildService.updateById(proBuild);
        if (flag)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }
}

